<?php
class PortalTagModel extends Model {
	var $tableName = 'portal_tag';
	
	function addTag($tagname, $aimId, $type='channel'){
		$tags = x('Xtag',array(),'Model')->getTagId($tagname);
		$tagArr = explode(',', $tags);
		
		$tagname	=	str_replace(array(' ','，',';','；'),',',$tagname);
		$tagnames	=	explode(',',$tagname);
		$map['type'] = $type;
		$map['aimId'] = $aimId;
		foreach($tagnames	as $k=>$v){
			$map['tagid'] = $tagArr[$k];			
			if( ! $tag = $this->where($map)->find() ){
				$map['tagname']	=	$v;
				$map['tagcount']	=	1;
				$res = $this->add($map);
				unset($map['tagname']);
				unset($map['tagcount']);
			}
		}

		return $res;
	}
	function delTag($aimId, $type='channel'){
		$map['type'] = $type;
		$map['aimId'] = array('in', $aimId);

		return $this->where($map)->delete();	
	}
	function getAllTag($type='channel'){
		$sql = "SELECT tagid, tagname, count(tagid) as tagcount FROM `ts_portal_tag` where type='$type' 
		        group by tagid order by tagcount desc";
		return $this->query($sql);	
	}
	function getTopTag($type='channel'){
		$sql = "SELECT tagid, tagname, count(tagid) as tagcount FROM `ts_portal_tag` where type='$type' 
		        group by tagid order by tagcount desc limit 0,10";
		return $this->query($sql);		
	}
}
?>